package gui;

import database.*;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class PanelAdminBestillinger extends JPanel implements ActionListener {
	private JScrollPane scrollPane;
	private DefaultTableModel tableModel;
	private JTable tabell;
	private JButton  buttonEndre, buttonSlette, buttonHovedmeny, buttonTilbake;
	private JLabel labelTittel;
	private List<Bestilling> bestillinger;
	private Vector<String> kolonnenavn;
	private GridBagConstraints constraints;
	
	public PanelAdminBestillinger(){
		this.setLayout(new GridBagLayout());
		this.leggTilKomponenter();
		GUI.getVindu().add(this);
		
	}
	private void leggTilKomponenter(){
		kolonnenavn = getKolonnenavn();
		tableModel = new DefaultTableModel(kolonnenavn,0){
			@Override
			public boolean isCellEditable(int row, int column) {
			       //all cells false
			       return false;
			}
		};
		fyllTableModel();
		
		tabell = new JTable(tableModel);
		tabell.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ListSelectionModel rad = tabell.getSelectionModel();
		rad.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				radSelected(e);
				
			}
		});
		tabell.getColumnModel().getColumn(0).setPreferredWidth(50);
		tabell.getColumnModel().getColumn(1).setPreferredWidth(80);
		tabell.getColumnModel().getColumn(2).setPreferredWidth(50);
		tabell.getColumnModel().getColumn(3).setPreferredWidth(240);
		
		
		buttonEndre = new JButton("Endre");
		buttonEndre.addActionListener(this);
		buttonEndre.setEnabled(false);
		
		buttonSlette = new JButton("Slette");
		buttonSlette.addActionListener(this);
		buttonSlette.setEnabled(false);
		
		buttonTilbake = new JButton("Tilbake");
		buttonTilbake.addActionListener(this);
		
		buttonHovedmeny = new JButton("Tilbake til hovedmeny");
		buttonHovedmeny.addActionListener(this);
		
		labelTittel = new JLabel("Bestillinger som ikke er levert");
		Font font = new Font(labelTittel.getFont().getFontName(),labelTittel.getFont().getStyle(),20);
		labelTittel.setFont(font);
		
		scrollPane = new JScrollPane(tabell);
		scrollPane.setMinimumSize(new Dimension(800,600));
		scrollPane.setPreferredSize(new Dimension(800, 600));
		
		
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.gridx = 0;
		constraints.weightx = 1;
		constraints.gridwidth =2;
		this.add(labelTittel,constraints);
		constraints.gridy = 1;
		this.add(scrollPane,constraints);
		constraints.gridy = 2;
		constraints.gridwidth = 1;
		constraints.gridx = 0;
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(buttonEndre,constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(buttonSlette,constraints);
		constraints.gridy = 3;
		constraints.gridx = 0;
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(buttonTilbake, constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(buttonHovedmeny, constraints);
	}

	private void buttonEndre_actionPerformed(ActionEvent e) 
	{
		this.setVisible(false);
		new PanelBestillingLevering(bestillinger.get(tabell.getSelectedRow()));
	}
	private void buttonSlette_actionPerformed(ActionEvent e) 
	{
		
		Databasecon.deleteBestilling((bestillinger.get(tabell.getSelectedRow()).getBestillingsID()));
		tableModel.removeRow(tabell.getSelectedRow());
	}
	private void buttonTilbake_actionPerformed(ActionEvent e) 
	{
		this.setVisible(false);
		new PanelAdmin();
	}
	private void buttonHovedmeny_actionPerformed(ActionEvent e) 
	{
		this.setVisible(false);
		new PanelHovedmeny();
	}

	private void radSelected(ListSelectionEvent e){
		
		if (tabell.getSelectedRows().length == 0) {
	        //No selection, disable fire button.
	            buttonEndre.setEnabled(false);
	            buttonSlette.setEnabled(false);

	        } else {
	        //Selection, enable the fire button.
	            buttonEndre.setEnabled(true);
	            buttonSlette.setEnabled(true);
	        }
		
		
		
	}
	private Vector<String> getKolonnenavn(){
		Vector<String> kolonnenavn = new Vector<String>();
		kolonnenavn.add("BestillingsID");
		kolonnenavn.add("Kunde");
		kolonnenavn.add("Retter");
		kolonnenavn.add("Leveringstidspunkt");
		return kolonnenavn;
	}
	private void fyllTableModel(){
		bestillinger = Register.findBestillingerKokk();
		bestillinger.addAll(Register.findBestillingerLevering());
		for (int i = 0; i < bestillinger.size(); i++) {
			Vector<String> radData = new Vector<String>();
			List<Rett> retter = Register.findRetter(bestillinger.get(i));
			String rettene = "";
			for (int j = 0; j < retter.size(); j++) {
				if(j==retter.size()-1){
					rettene += retter.get(j).getNavn();
				}
				else{
					rettene += retter.get(j).getNavn() + ", ";					
				}
			}
			radData.add(Integer.toString(bestillinger.get(i).getBestillingsID()));
			radData.add(Register.findKundeObjekt(bestillinger.get(i).getKundeID()).getfNavn() + " " + Register.findKundeObjekt(bestillinger.get(i).getKundeID()).geteNavn());
			radData.add(rettene);
			radData.add(bestillinger.get(i).getLeveringstidspunkt());
			tableModel.addRow(radData);
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonEndre)
			buttonEndre_actionPerformed(e);
		else if(e.getSource()==buttonSlette)
			buttonSlette_actionPerformed(e);
		else if(e.getSource()==buttonTilbake)
			buttonTilbake_actionPerformed(e);
		else if(e.getSource()==buttonHovedmeny)
			buttonHovedmeny_actionPerformed(e);
		
	}
}
